package com.ocom.aiot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.aiot.dao.IDaoOtaVerInfo;
import com.ocom.aiot.service.OtaVerInfoService;
import com.ocom.common.entity.aiot.EntityOtaVerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OtaVerInfoServiceImpl extends ServiceImpl<IDaoOtaVerInfo, EntityOtaVerInfo> implements OtaVerInfoService {

    @Autowired
    private IDaoOtaVerInfo iDaoOtaVerInfo;

    @Override
    public int saveOtaFile(String appVer, String url, Integer type) {
        EntityOtaVerInfo entityOtaVerInfo=new EntityOtaVerInfo();
        entityOtaVerInfo.setAppVer(appVer);
        entityOtaVerInfo.setApkFileUrl(url);
        entityOtaVerInfo.setType(type);
        Date date=new Date();
        entityOtaVerInfo.setUpdateTime(date);
        entityOtaVerInfo.setCreateTime(date);
        return iDaoOtaVerInfo.insert(entityOtaVerInfo);
    }

    @Override
    public List<EntityOtaVerInfo> otaFileList(Integer page, Integer size) {
        QueryWrapper<EntityOtaVerInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		if(size == 0){
			size=1000;
		}
        PageHelper.startPage(page, size);
        return this.list(queryWrapper);
    }


	@Override
	public EntityOtaVerInfo selectAppVerInfo(String appVer) {
		QueryWrapper<EntityOtaVerInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("status",1);
		queryWrapper.eq("app_ver",appVer);
		queryWrapper.orderByDesc("create_time");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

}
